#include <graphics.h>
#include <conio.h>
#include <stdio.h>

int main()
{
	float width, height, gravity;
	width = 600;
	height = 600;
	gravity = 0.4;
	initgraph(width, height);

	float radius, ball_x, ball_y, ball_vy;
	radius = 15;
	ball_x = width / 4;
	ball_y = height /2;
	ball_vy = 0;

	float rect_left_x, rect_top_y, rect_width, rect_height;
	float rect_top_y2;

	float rect_vx;
	rect_height = 100;
	rect_width = 20;
	rect_left_x = width * 3 / 4;
	rect_top_y = height - rect_height;
	rect_top_y2 = 0;
	rect_vx = -3;

	int score = 0;


	while (1)
	{
		if (_kbhit())
		{
			char input = _getch();
			if (input == ' ' ) //输入空格
			{
				ball_vy = -10;
				
			}
		}


		ball_vy = ball_vy + gravity;
		ball_y = ball_y + ball_vy;
		if (ball_y >= height - radius)
		{
			ball_y = 0;
			ball_y = height - radius;
			 
		}
		rect_left_x = rect_left_x + rect_vx;
		if (rect_left_x <= 0)
		{
			score = score + 1;
			rect_left_x = width;
			rect_height = rand() % int(height / 5) + height / 5;//随机高度
			rect_vx = rand() / float(RAND_MAX) * 4 - 6;//随机速度
		}
		//如果小球碰到方块
		if ((rect_left_x <= ball_x + radius)
			&& (rect_left_x + rect_width >= ball_x - radius)
			&& (height - rect_height <= ball_y + radius))
		{
			score = -1;
			Sleep(100);
		}
		if ((rect_left_x <= ball_x + radius)
			&& (rect_left_x + rect_width >= ball_x - radius)
			&& ( rect_height>= ball_y - radius))
		{
			score = -1;
			Sleep(100);
		}


		cleardevice();

		fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);
		fillrectangle(rect_left_x, 0, rect_left_x + rect_width, rect_height );
		fillcircle(ball_x, ball_y, radius);

		TCHAR s[20];//定义字符串数组
		_stprintf_s(s, _T("%d"), score);//将score转化为字符串
		settextstyle(40, 0, _T("宋体"));//设置字体大小，字体
		outtextxy(50, 30, s);//输出得分文字

		Sleep(10);


	}
	_getch();
	closegraph();
	return 0;

}